﻿using GodSoft.Common;
using GodSoft.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GodSoft.Controllers
{
    public class LampControllerController : BaseController
    {

        GodSoftContext _context = new GodSoftContext();

        public ActionResult GetLightComboboxList()
        {
            //总的路灯
            var SumLight = from i in _context.Lights
                           orderby i.ID
                           select i.ID;
            //控制器已经绑定的路灯
            var BindLight = from i in _context.LampControllers
                            orderby i.LightID
                            select i.LightID;
           //控制器还可以绑定的路灯
           var EnableLight = SumLight.Except(BindLight).ToList();//求差集

            var list = from i in _context.Lights
                       orderby i.ID
                       where EnableLight.Contains(i.ID)
                       select new
                       {
                           LightID = i.ID,
                           LightName = i.LightName
                       };

            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetControllerTypeComboboxList()
        {
            var list = from i in _context.DeviceTypes
                       orderby i.ID
                       select new
                       {
                           ControllerTypeID = i.ID,
                           ControllerTypeName = i.Type_Name
                       };

            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetCollectorComboboxList()
        {
            var list = from i in _context.Collectors
                       orderby i.ID
                       select new
                       {
                           CollectorID = i.ID,
                           CollectorName = i.Name
                       };

            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Index(int? CollectorID, int? LampControllerID)
        {
            if (CollectorID != null)
            {
                ViewBag.CollectorID = CollectorID;
            }
            if (LampControllerID != null)
            {
                ViewBag.LampControllerID = LampControllerID;
            }
            return View();
        }
        [HttpPost]
        public ActionResult Regist(LampController LampController)
        {
            _context.LampControllers.Add(LampController);
            _context.SaveChanges();
            return Content("OK");
        }

        public ActionResult GetAll(int? CollectorID, int? LampControllerID)
        {
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            if (LampControllerID != null)
            {
                var temp = from i in _context.LampControllers
                           from j in _context.Lights
                           from k in _context.Collectors
                           from l in _context.DeviceTypes
                           where i.LightID==j.ID&&i.CollectorID==k.ID&&i.ControllerTypeID==l.ID&&i.ID == LampControllerID
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.Controller_Name,
                               i.Controller_Address,
                               i.Controller_Status,
                               i.Enable,
                               i.ControllerTypeID,//控制器类型编号
                               l.Type_Name,//控制器类型名
                               i.CollectorID,//采集器编号
                               k.Name,//采集器名
                               i.LightID,//控制器控制的路灯编号
                               j.LightName,//控制器控制的路灯名
                               i.Notes
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else if (CollectorID != null)
            {
                var temp = from i in _context.LampControllers
                           from j in _context.Lights
                           from k in _context.Collectors
                           from l in _context.DeviceTypes
                           where i.LightID == j.ID && i.CollectorID == k.ID && i.ControllerTypeID == l.ID && i.CollectorID == CollectorID
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.Controller_Name,
                               i.Controller_Address,
                               i.Controller_Status,
                               i.Enable,
                               i.ControllerTypeID,//控制器类型编号
                               l.Type_Name,//控制器类型名
                               i.CollectorID,//采集器编号
                               k.Name,//采集器名
                               i.LightID,//控制器控制的路灯编号
                               j.LightName,//控制器控制的路灯名
                               i.Notes
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else {
                var temp = from i in _context.LampControllers
                           from j in _context.Lights
                           from k in _context.Collectors
                           from l in _context.DeviceTypes
                           where i.LightID == j.ID && i.CollectorID == k.ID && i.ControllerTypeID == l.ID 
                           orderby i.ID
                           select new
                           {
                               i.ID,
                               i.Controller_Name,
                               i.Controller_Address,
                               i.Controller_Status,
                               i.Enable,
                               i.ControllerTypeID,//控制器类型编号
                               l.Type_Name,//控制器类型名
                               i.CollectorID,//采集器编号
                               k.Name,//采集器名
                               i.LightID,//控制器控制的路灯编号
                               j.LightName,//控制器控制的路灯名
                               i.Notes
                           };
                var list = temp.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                var result = new { total = temp.Count(), rows = list };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
           

        }

        public ActionResult UpdateInfo(LampController LampController)
        {
            _context.LampControllers.Attach(LampController);
            _context.Entry<LampController>(LampController).State = EntityState.Modified;
            _context.SaveChanges();
            return Content("OK");
        }

        public ActionResult DeleteInfo(string deleteInfoID)
        {
            if (string.IsNullOrEmpty(deleteInfoID))
            {
                return Content("请选择您要删除的数据");
            }
            //截取传递过来的字符串的数字信息
            var idsStr = deleteInfoID.Split(',');

            List<int> deleteIDList = new List<int>();

            foreach (var ID in idsStr)
            {
                deleteIDList.Add(Convert.ToInt32(ID));
            }
            if (DeleteManyInfoes(deleteIDList) > 0)
            {
                return Content("OK");
            }
            else
            {
                return Content("删除失败，请您检查");
            }

        }

        public int DeleteManyInfoes(List<int> DeleteIDList)
        {
            foreach (var id in DeleteIDList)
            {
                LampController LampController = _context.LampControllers.Where(f => f.ID == id).FirstOrDefault();
                _context.LampControllers.Remove(LampController);
            }
            return _context.SaveChanges();
        }
   

    }
}
